﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

using NotaManager;

namespace ConsoleUtility
{
    class Program
    {
        static void Main(string[] args)
        {
            var manager = new DafaultNotaManager();

            if (args.Length != 3)
            {
                Console.WriteLine("Need 3 arguments for processing: ");
                Console.WriteLine("login_on_nota password_on_nota path_to_file");

                Console.ReadKey();
                return;
            }

            var login = args[0];
            var pass = args[1];
            var filepath = args[2];

            Console.WriteLine("Your login: {0}", login);
            Console.WriteLine("Your password: {0}", pass);
            Console.WriteLine("Your file: {0}", filepath);
           
            Console.WriteLine("Starting...");
            Console.WriteLine("Be wary: Aborting or crashing process will lead to what your dowloading progress will be lost!");

            var res = manager.Login(login, pass).Result;

            if (res.Status == RequestStatus.ConnectionError)
            {
                Console.WriteLine("Login to Nota unSuccesful! Check your network...");
                Console.ReadKey();
                return;
            }
            else if(res.Status == RequestStatus.ServerRefuse)
            {
                Console.WriteLine("Login to Nota unSuccesful! Check your data (login or pass)...");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Login to Nota succesful! Downloading...");
            var script = manager.GetTranslation(callback).Result;
            var temp = new XmlSerializer(typeof(Script));
            temp.Serialize(System.IO.File.OpenWrite(filepath), script);

            //var rememberManager = new Remember11Manager();
            //var str = rememberManager.GetCurrentText();

            Console.WriteLine("All done! Press any key to exit...");
            Console.ReadKey();
        }

        static void callback(double d)
        {
            Console.Write("\r Proceed: {0}%", (int)d);
        }
    }
}
